import logging

import requests
from config import URL

ses = requests.session()


class RegLoginApi:

    def __init__(self, session):
        self.ses = session

    # 获取图片验证码接口
    def get_img_verify_code(self, r):
        resp = self.ses.get(url=URL + "/common/public/verifycode1/{}".format(r))
        print(f"获取图片验证码返回的状态码{resp.status_code}")
        logging.info(f"获取图片验证码返回的状态码{resp.status_code}")
        return resp

    # 获取短信验证码接口
    def get_phone_verify_code(self, phone_v, imgVerifyCode_v, type_v):
        form_dict = {"phone": phone_v, "imgVerifyCode": imgVerifyCode_v, "type": type_v}
        resp = self.ses.post(url=URL + "/member/public/sendSms", data=form_dict)
        print("获取短信验证码接口的响应体数据", f"{resp.json()}")
        logging.info(f"获取短信验证码接口的响应体数据{resp.json()}")
        return resp

    # 注册接口
    def user_register(self, phone_v, password_v, verifycode_v, phone_code_v, dy_server_v, invite_phone_v):
        form_dict = {"phone": phone_v, "password": password_v, "verifycode": verifycode_v,
                     "phone_code": phone_code_v, "dy_server": dy_server_v, "invite_phone": invite_phone_v}
        resp = self.ses.post(url=URL + "/member/public/reg", data=form_dict)
        print("获取注册接口的响应体数据", f"{resp.json()}")
        logging.info(f"获取注册接口的响应体数据{resp.json()}")
        return resp

    # 登录接口
    def user_login(self, username_v, pwd_v):
        form_dict = {"keywords": username_v, "password": pwd_v}
        resp = self.ses.post(url=URL + "/member/public/login", data=form_dict)
        print("获取登录接口的响应体数据", f"{resp.json()}")
        logging.info(f"获取登录接口的响应体数据{resp.json()}")
        return resp


if __name__ == '__main__':
    a = RegLoginApi(ses)
    resp = a.get_img_verify_code(123)
    print(resp.status_code)
    print("=" * 50)

    resp = a.get_phone_verify_code("18373444577", "8888", "reg")
    print(resp.json())
    print("=" * 50)

    resp = a.user_register("18373444577", "11111MM", "8888", "666666", "on", "18376789000")
    print(resp.json())
    print("=" * 50)

    resp = a.user_login("18373444511", "11111MM")
    print(resp.json())
    print("=" * 50)
